package com.cjjd.c.feature.mine.information;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.BaseSwipeRefreshFragment;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.ContactBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.FragmentUtil;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/25.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ContactFragment extends BaseSwipeRefreshFragment {
    @Override
    public Object getTitleString() {
        return null;
    }



    private ListResult<ContactBean> bean;
    @Override
    public void responseSuccess(Call<HttpResult> call, Response<HttpResult> response) {
        bean = (ListResult<ContactBean>) response.body().getData();
        setDataList(bean.dataList);
    }

    @Override
    protected void initView() {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.item_header_information, listBase, false);
        TextView tvName = (TextView) headerView.findViewById(R.id.tv_name);
        tvName.setText("新增常用联系人");

        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtil.replaceWithAddToBack(mContext, new ContactAddFragment(), R.id.fragment_container);
            }
        });
        listBase.addHeaderView(headerView);
        listBase.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ContactInforFragment contactInforFragment = new ContactInforFragment();
                contactInforFragment.setUuid(bean.dataList.get(position-1).uuid);
                LogUtil.e("xyd","positon is " + position);
                LogUtil.e("xyd","uuid id" + bean.dataList.get(position-1).uuid);
                FragmentUtil.replaceWithAddToBack(mContext, contactInforFragment, R.id.fragment_container);
            }
        });
        super.initView();
    }

    @Override
    public MyBaseAdapter getAdapter() {
        return new InformationAdapter(mContext,InformationAdapter.CONTACT);
    }

    @Override
    public Call<HttpResult> getCall() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("nowPage", PAGE + "");
        h1.put("pageShow", PAGE_SIZE + "");
        return service.getMemberContactList(JsonUtils.convertParam("getMemberContactList", h1));
    }
}
